﻿#include <iostream>
#include <fstream>
#include "HttpRequestTool.hpp"
#include <Windows.h>
using namespace std;

// 下载中的回调函数
void DownCallBack(INT64 downloadSize, INT64 totalSize) {
	/*cout << "总大小：" << totalSize << "字节，";
	cout << "已下载：" << downloadSize << "字节，" << endl;*/
	float progress = float(downloadSize * 10000 / totalSize) / 100;
	cout << "\r" << "下载进度：" << progress << "%," << std::flush;
}

// 下载结束的回调函数
void DownSuccessCallBack(string& buf) {
	cout << "下载完毕：" << buf.size() << "字节" << endl;
}

int main()
{
	CHttpRequestTool CHttp;

	//// 其它测试
	//CHttp.setHeader(CHttp.defaultHeader);
	//string res = CHttp.HttpGet("https://xui.ptlogin2.qq.com/cgi-bin/xlogin?appid=636014201&s_url=http://www.qq.com/qq2012/loginSuccess.htm&style=20&border_radius=1&target=self&maskOpacity=40");
	//if(CHttp.getCode()!=200) {
	//	cout << "失败1：" << CHttp.getCode() << endl;
	//}
	//else {
	//	string cookies = CHttp.getHeader("Set-Cookie");
	//	int find1 = cookies.find("pt_local_token");
	//	int find2 = cookies.find(";", find1);
	//	if (find1 == -1 || find2 == -1) {
	//		cout << "获取cookie失败：" << CHttp.getCode() << endl;
	//		return 0;
	//	}
	//	string pt_local_token = cookies.substr(find1 + 15, find2 - find1 - 15);
	//	CHttp.setHeader(CHttp.defaultHeader + "Referer:https://xui.ptlogin2.qq.com/cgi-bin/xlogin?appid=636014201&s_url=http%3A%2F%2Fwww.qq.com%2Fqq2012%2FloginSuccess.htm\r\n");
	//	res = CHttp.HttpGet("https://localhost.ptlogin2.qq.com:4301/pt_get_uins?callback=ptui_getuins_CB&r=0.65213949938979&pt_local_tk=" + pt_local_token);
	//	if (CHttp.getCode() != 200) {
	//		cout << "失败2：" << CHttp.getCode() << endl;
	//	}
	//	else {
	//		cout << "状态码：\n" << CHttp.getCode() << endl;
	//		cout << "data：\n" << res << endl;
	//		cout << "响应头：\n"; cout << CHttp.getHeader("", 1) << endl;
	//	}
	//}
	//system("pause");

	//// 下载文件, 添加回调（不建议大于2G的文件）
	// 隐藏光标 // 
	HANDLE console = GetStdHandle(STD_OUTPUT_HANDLE);
	CONSOLE_CURSOR_INFO cursorInfo;
	GetConsoleCursorInfo(console, &cursorInfo);
	cursorInfo.bVisible = false;  
	SetConsoleCursorInfo(console, &cursorInfo);
	// 隐藏光标 // 
	std::string buf;
	CHttp.connect(DownCallBack);
	CHttp.connect(DownSuccessCallBack);
	buf = CHttp.HttpDown("https://dl.360safe.com/se/360se_setup.exe");
	system("pause");

	//	//只获取响应标头，不获取具体数据&不下载文件
	//CHttp.HttpHeader("http://winchan.top/1.php");
	//cout << CHttp.getHeader("Date") << endl;

	// //设置请求头, GET请求
	//CHttp.setHeader(CHttp.defaultHeader+"Content-Type:  text/html; charset=发的好奇UTF-8\r\n");
	//string res = CHttp.HttpGet("http://winchan.top/1.php");
	//cout << "状态码：\n" << CHttp.getCode() << endl;
	//cout << "data：\n" << res << endl;
	//cout << "响应头：\n"; cout << CHttp.getHeader("", 1) << endl;

	// // POST请求
	//string res2 = CHttp.HttpPost("http://winchan.top/1.php", "el=2&file=dsag方可和我七分文化&sign=fdiwqyhfiewqj==");
	//cout << "状态码：\n" << CHttp.getCode() << endl;
	//cout << "data：\n" << res << endl;
	//cout << "响应头：\n"; cout << CHttp.getHeader("", 1) << endl;

	// 下载文件, 保存文件（不建议大于2G的文件）
	/*cout << "下载中..." << endl;
	string strData = CHttp.HttpDown("https://dl.360safe.com/360zip_setup.exe");
	string fileSizeStr = CHttp.getHeader("Content-Length");
	if (fileSizeStr.empty() || strData.empty()) {
		cout << "下载失败：" << CHttp.getCode() << endl;
		return 0;
	}
	DWORD64 fileSize = stoull(fileSizeStr);
	ofstream out(L"C:\\360zip_setup.exe", ios::binary);
	if (out.is_open()) {
		out.write(strData.c_str(), strData.size());
		out.close();
	}
	cout << "下载成功：" << CHttp.getCode() << endl;
	cout << "Content-Length大小：" << fileSize << "字节，buf大小：" << strData.size() << endl;*/

	return 0;
}